{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Evaluate trained DeepParticleNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "\n",
    "from imgaug import augmenters as iaa\n",
    "from keras.callbacks import EarlyStopping\n",
    "\n",
    "# Add root directory to the python search path, if it is not already in there.\n",
    "root_dir = os.path.abspath(\"..\")\n",
    "if root_dir not in sys.path:\n",
    "    sys.path.append(root_dir)\n",
    "\n",
    "# Import the DeepParticleNet modules.\n",
    "from dpn.config import Config\n",
    "from dpn.dataset import Dataset\n",
    "from dpn.model import Model\n",
    "from dpn.sizedistribution import SizeDistribution as PSD\n",
    "from dpn import visualize\n",
    "\n",
    "from external.CLR.clr_callback import CyclicLR\n",
    "\n",
    "%matplotlib inline \n",
    "\n",
    "dataset_path = os.path.join(root_dir, \"datasets\")\n",
    "dataset_name = \"mpac\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "class ApplicationConfig(Config):\n",
    "    NAME = \"Application\"\n",
    "    BACKBONE = \"resnet101\"\n",
    "    USE_PRETRAINED_WEIGHTS = \"mpac\"\n",
    "    GPU_COUNT = 1\n",
    "    IMAGES_PER_GPU = 1\n",
    "    BATCH_SIZE = 1\n",
    "    DETECTION_MIN_CONFIDENCE = 0.5\n",
    "    DETECTION_NMS_THRESHOLD = 0.3\n",
    "    MEAN_PIXEL = [60.2, 60.2, 60.2]\n",
    "    \n",
    "config = ApplicationConfig()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "model_dir = os.path.join(root_dir, \"pretrained_weights\")\n",
    "\n",
    "# Create model in inference mode\n",
    "model = Model(mode=\"inference\", \n",
    "              model_dir=model_dir,\n",
    "              config=config)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Test Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Inherit from the Dataset class.\n",
    "class MpacDataset(Dataset):\n",
    "    MONOCLASS = \"sphere\"  # The dataset only has one class.\n",
    "\n",
    "# Test dataset.\n",
    "dataset_name = \"test_s06\"\n",
    "dataset = MpacDataset()\n",
    "dataset.load_dataset(dataset_path, dataset_name)\n",
    "dataset.prepare()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run Detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = model.analyze_dataset(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.display_detection_image(2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
